package com.fridge.shopping;

import java.util.List;

import com.fridge.data.DBHelper;
import com.fridge.data.ShoppingItem;
import com.id2216.fridge.R;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;



public class ShoppingAdapter extends ArrayAdapter<ShoppingItem> {

    Context context;
    int layoutResourceId;   
    List<ShoppingItem> data = null;
    
	public ShoppingAdapter(Context context, int resource, List<ShoppingItem> shoppingItemList) 
	{
		super(context, resource, shoppingItemList);
		
        this.layoutResourceId = resource;
        this.context = context;
        this.data = shoppingItemList;
        this.context = context;
 	}
	
	
	@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View row = convertView;
        ShoppingHolder holder = null;
        final ShoppingItem item = data.get(position);

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new ShoppingHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtQuantity = (TextView)row.findViewById(R.id.txtQuantity);  
            holder.checkShoppingDone = (CheckBox)row.findViewById(R.id.shoppingCheckBox);
            
            holder.checkShoppingDone.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    
                	DBHelper db = new DBHelper(context);
                	if(isChecked)
                	{
                		Log.d("Shopping", "YES");
                		item.setStatus(ShoppingItem.STATUS_DONE);
                	}
                	else
                		item.setStatus(ShoppingItem.STATUS_DEFAULT);
                	db.updateShoppingItem(item);
                	db.close();
                	
                	
                }
            });
           
            //row.setOnClickListener(new onItemClick(position));
            row.setTag(holder);
        }
        else
        {
            holder = (ShoppingHolder)row.getTag();
        }
       
        holder.txtTitle.setText(item.getName());
        holder.txtQuantity.setText(item.getQuantityString());

        if(item.getStatus() == ShoppingItem.STATUS_DONE)
        	holder.checkShoppingDone.setChecked(true);
        else
        	holder.checkShoppingDone.setChecked(false);
        	

        return row;
    }
	
	static class ShoppingHolder
	{
		CheckBox checkShoppingDone;
		TextView txtTitle;
		TextView txtQuantity;
	}
}